module.exports = {
  // 获取获取用户权益
  getMemberCardsWithRights(){
    wx.$showLoading()
    wx.$methods.getUserInfo().then(res=>{
      this.setData({
        bindPhone:!!res.result.phone,
        account:res.result.account,
       
      })
      if(res.result.account.plan&& res.result.account.plan['level']){
        this.setData({
          level:res.result.account.plan['level']
        })
      }
 

    wx.$methods.getMemberCardsWithRights().then(res=>{
      wx.hideLoading()
      wx.stopPullDownRefresh()
      let vip = res.result[1]
      vip.plans.forEach((item,index)=>{
        if(index==0){
          item.bg = 'https://static.dingtalk.com/media/lALPDefR9WLBqHjNATbNAnY_630_310.png'
          item.icon = 'https://static.dingtalk.com/media/lALPDfYH7_XfGZlITA_76_72.png'
        }
        if(index==1){
          item.bg = 'https://static.dingtalk.com/media/lALPDeC2-Bk4AB_NATbNAnY_630_310.png'
          item.icon = 'https://static.dingtalk.com/media/lALPDfmVbpqq6yxITA_76_72.png'
        }
        if(index==2){
          item.bg = 'https://static.dingtalk.com/media/lALPDe7s8qxQVg7NATbNAnY_630_310.png'
          item.icon = 'https://static.dingtalk.com/media/lALPDf0i7T9s37xITA_76_72.png'
        }
        if(index==3){
          item.bg = 'https://static.dingtalk.com/media/lALPDetfdAd06a7NATbNAnY_630_310.png'
          item.icon = 'https://static.dingtalk.com/media/lALPDfJ6cVEWipVITA_76_72.png'
        }
        
        return item
      })
      vip.plans = vip.plans.filter(item=>{
        console.log('this.data.level',this.data.level)
        return item.level >= this.data.level
      })
      
      this.setData({
        plain:vip
      })
      console.log(res,'ddddd')
    })
  })
  },

  // 会员卡支付
  payForMemberCard(planId,subscribedPeriod) {
    let t = {
        "planId": planId,
        "subscribedPeriod": subscribedPeriod
    }
    wx.$methods.createOrder(t).then(res => {
      wx.$methods.wechatPay({orderSn:res.result.orderSn}).then(res=>{
        wx.requestPayment({
          timeStamp: res.result.timeStamp,
          nonceStr: res.result.nonceStr,
          package: res.result.packageValue,
          signType: res.result.signType,
          paySign: res.result.paySign,
          success:res=>{
            wx.showLoading({
              title: '会员升级中',
              duration:2000
            })
            setTimeout(()=>{
              wx.hideLoading()
              wx.showToast({
                title: '会员升级成功',
              })
              
              this.getMemberCardsWithRights()
             
            },2000)
            
          }
        })
      })
   
    })
  },
  // 绑定用户手机
  bindPhone(t,index) {
    wx.$methods.bindPhone(t).then(res => {
      this.setData({
        bindPhone:true
      })
      let { id,subscribedPeriod } = this.data.plain.plains[index]
      this.payForMemberCard(id,subscribedPeriod)
    })
  },
  // 获取用户信息, 此方法只能在支付成功后调用,用户判断用户是否升级为限量卡会员
  getUserInfo(){
    wx.$methods.getUserInfo().then(res=>{
        this.setData({
          bindPhone:!!res.result.phone,
          account:res.result.account,
         
        })
        if(res.result.account.plan&& res.result.account.plan['level']){
          this.setData({
            level:res.result.account.plan['level']
          })
        }
    })
  },

  // 查看福袋领取记录
  getLuckyBugRecevingRecord() {
    wx.$methods.getLuckyBugRecevingRecord().then(res => {
      if (!res.body.hasRecord) {
        wx.showModal({
          title: '',
          content: '大美将送您一份福袋，但还不知道您的收货地呢',
          confirmText: "领福袋",
          cancelText: "下次再说",
          confirmColor: "#F61B6D",
          success: res => {
            if (res.confirm) {
              // 填写福宝地址
              this.onPushToLuckyBag()
            }
          }
        })
      }
    })
  }
}